package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.OmsOrderProduct;
import com.ruoyi.system.domain.PmsProduct;
import com.ruoyi.system.domain.vo.OrderDeptExcel;
import com.ruoyi.system.domain.vo.OrderVo;
import com.ruoyi.system.mapper.OmsOrderProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.OmsOrderMapper;
import com.ruoyi.system.domain.OmsOrder;
import com.ruoyi.system.service.IOmsOrderService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 订单Service业务层处理
 *
 * @author ruoyi
 * @date 2024-11-29
 */
@Service
public class OmsOrderServiceImpl implements IOmsOrderService
{
    @Autowired
    private OmsOrderMapper omsOrderMapper;

    @Autowired
    private OmsOrderProductMapper omsOrderProductMapper;



    /**
     * 查询订单
     *
     * @param orderId 订单主键
     * @return 订单
     */
    @Override
    public OmsOrder selectOmsOrderByOrderId(Long orderId)
    {
        return omsOrderMapper.selectOmsOrderByOrderId(orderId);
    }

    /**
     * 查询订单列表
     *
     * @param omsOrder 订单
     * @return 订单
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<OmsOrder> selectOmsOrderList(OmsOrder omsOrder)
    {
        return omsOrderMapper.selectOmsOrderList(omsOrder);
    }

    /**
     * 新增订单
     *
     * @param omsOrder 订单
     * @return 结果
     */
    @Override
    public int insertOmsOrder(OmsOrder omsOrder)
    {
        omsOrder.setCreateTime(DateUtils.getNowDate());
        return omsOrderMapper.insertOmsOrder(omsOrder);
    }

    /**
     * 修改订单
     *
     * @param omsOrder 订单
     * @return 结果
     */
    @Override
    public int updateOmsOrder(OmsOrder omsOrder)
    {
        omsOrder.setUpdateTime(DateUtils.getNowDate());
        return omsOrderMapper.updateOmsOrder(omsOrder);
    }

    /**
     * 批量删除订单
     *
     * @param orderIds 需要删除的订单主键
     * @return 结果
     */
    @Override
    public int deleteOmsOrderByOrderIds(Long[] orderIds)
    {
        return omsOrderMapper.deleteOmsOrderByOrderIds(orderIds);
    }

    /**
     * 删除订单信息
     *
     * @param orderId 订单主键
     * @return 结果
     */
    @Override
    public int deleteOmsOrderByOrderId(Long orderId)
    {
        return omsOrderMapper.deleteOmsOrderByOrderId(orderId);
    }


    @Override
    public int addOrderProducts(OrderVo omsOrder) {
        OmsOrder order = omsOrder.getOmsOrder();
        int j = insertOmsOrder(order);
        if(j>0){
            if(!omsOrder.getOrderProductList().isEmpty() && omsOrder.getOrderProductList().size()>0){
                List<OmsOrderProduct> orderProducts = omsOrder.getOrderProductList();
                for (int i=0;i<orderProducts.size();i++){
                    orderProducts.get(i).setOrderId(order.getOrderId());
                    omsOrderProductMapper.insertOmsOrderProduct(orderProducts.get(i));
                }
            }
        }
        return j;
    }


    @Override
    public List<OrderVo> getOrderProductList(OmsOrder omsOrder) {
        return omsOrderMapper.getOrderProductList(omsOrder);
    }


//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public int updateOrderProducts(OrderVo omsOrder) {
//
//        BigDecimal total = BigDecimal.ZERO;
//        if(omsOrder.getOmsOrder().getOrderId() == null){
//          return  addOrderProducts(omsOrder);
//        }
//
//        int j= updateOmsOrder(omsOrder.getOmsOrder());
//        OmsOrderProduct o = new OmsOrderProduct();
//        o.setOrderId(omsOrder.getOmsOrder().getOrderId());
//        List<OmsOrderProduct> getList = omsOrderProductMapper.selectOmsOrderProductList(o);
//        Iterator<OmsOrderProduct> it = getList.iterator();
//
//
//        List<OmsOrderProduct> updateList = omsOrder.getOrderProductList();
//        List<OmsOrderProduct> addList = new ArrayList<>();
//        List<OmsOrderProduct> editList = new ArrayList<>();
//        List<OmsOrderProduct> delList = new ArrayList<>();
//        for(int i = 0;i<updateList.size();i++){
////            boolean isDe = false;
////            while(it.hasNext()) {
////                if(updateList.get(i).getOmsOrderProductId().equals(it.next().getOmsOrderProductId())){
////                    editList.add(updateList.get(i));
////                    isDe = true;
////                }
////            }
//
//            if(null == updateList.get(i).getOmsOrderProductId()){
//                addList.add(updateList.get(i));
//            }
//            else{
//                while (it.hasNext()){
//                    OmsOrderProduct omsOrderProduct = it.next();
//                    if(!updateList.stream().anyMatch(obj->obj.getOmsOrderProductId().equals(omsOrderProduct.getOmsOrderProductId()))){
//                        delList.add(omsOrderProduct);
//                    }
//                    else {
//                        for (OmsOrderProduct orderProduct : updateList){
//                            if(null!= orderProduct.getOmsOrderProductId() && orderProduct.getOmsOrderProductId().equals(omsOrderProduct.getOmsOrderProductId())){
//                                editList.add(orderProduct);
//                            }
//                        }
//
//                    }
//                }
//            }
//
//        }
//
//
//        if(addList.size()>0){
//            try {
//                omsOrderProductMapper.insertOmsOrderProductList(addList);
//            }
//            catch (Exception e){
//
//            }
//
//        }
////        getList.removeAll(addList);
//        if(delList.size()>0){
//            try {
//                omsOrderProductMapper.delOmsOrderProductList(delList);
//            }
//            catch (Exception e){
//
//            }
//        }
//        if(editList.size()>0){
//            try {
//                omsOrderProductMapper.updateOmsOrderProductList(editList);
//            }
//           catch (Exception e){
//
//           }
//        }
//        return j;
//    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateOrderProducts(OrderVo omsOrder) {

        BigDecimal total = BigDecimal.ZERO;
        if(omsOrder.getOmsOrder().getOrderId() == null){
            return  addOrderProducts(omsOrder);
        }

        int j= updateOmsOrder(omsOrder.getOmsOrder());
        OmsOrderProduct o = new OmsOrderProduct();
        o.setOrderId(omsOrder.getOmsOrder().getOrderId());

        List<OmsOrderProduct> updateList = omsOrder.getOrderProductList();
        List<OmsOrderProduct> oldList = omsOrderProductMapper.selectOmsOrderProductList(o);
        // 生成ID映射集
        Set<Long> updateIds = updateList.stream()
                .map(OmsOrderProduct::getOmsOrderProductId)
                .filter(Objects::nonNull)
                .collect(Collectors.toSet());


        List<OmsOrderProduct> addList = new ArrayList<>();
        List<OmsOrderProduct> editList = new ArrayList<>();
        List<OmsOrderProduct> delList = new ArrayList<>();

// 1. 找出待删除：原有不在新列表中的商品
        for (OmsOrderProduct oldItem : oldList) {
            if (!updateIds.contains(oldItem.getOmsOrderProductId())) {
                delList.add(oldItem);
            }
        }

// 2. 找出新增/更新
        for (OmsOrderProduct newItem : updateList) {
            if (newItem.getOmsOrderProductId() == null) {
                addList.add(newItem); // 无ID → 新增
            } else if (updateIds.contains(newItem.getOmsOrderProductId())) {
                editList.add(newItem); // 有ID且在更新集 → 修改
            }
        }



        if(addList.size()>0){
                omsOrderProductMapper.insertOmsOrderProductList(addList);
        }
//        getList.removeAll(addList);
        if(delList.size()>0){

                omsOrderProductMapper.delOmsOrderProductList(delList);


        }
        if(editList.size()>0){
                omsOrderProductMapper.updateOmsOrderProductList(editList);
        }
        return j;
    }

    @Override
//    @DataScope(deptAlias = "d", userAlias = "u")
    public List<OrderDeptExcel> selectOmsOrderExportExcel(OmsOrder omsOrder) {
        return omsOrderMapper.selectOmsOrderExportExcel(omsOrder);
    }
}
